﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

namespace BasketStats.Core.Team
{
    public enum Role { UNDEFINED, PLAYMAKER, GUARD, FORWARD, CENTER };

    public class PlayerInfo : IXmlSerializable
    {
        public string Name { get; set; }
        public string FamilyName { get; set; }
        public uint Number { get; set; }
        public Role Role { get; set; }

        private IDictionary<string, Role> m_roleMapping = new Dictionary<string, Role>
        {
            {"UNDEFINED", Role.UNDEFINED},
            {"PLAYMAKER", Role.PLAYMAKER},
            {"GUARD", Role.GUARD},
            {"FORWARD", Role.FORWARD},
            {"CENTER", Role.CENTER},
        };

        public static PlayerInfo Empty
        {
            get
            {
                return new PlayerInfo { Name = string.Empty, FamilyName = string.Empty, Role = Role.UNDEFINED, Number = 0 };
            }
        }

        public PlayerInfo()
        { 
        }

        public PlayerInfo(XmlReader reader)
        {
            ReadXml(reader);
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Name = reader.GetAttribute("name");
            FamilyName = reader.GetAttribute("familyname");
            Number = uint.Parse(reader.GetAttribute("number"));
            Role = m_roleMapping[reader.GetAttribute("role")];
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("name", Name.ToString());
            writer.WriteAttributeString("familyname", FamilyName.ToString());
            writer.WriteAttributeString("role", Role.ToString());
            writer.WriteAttributeString("number", Number.ToString());
        }
    }
}
